import { reqgetbanner, reqgetcate, reqgetindexgoods, reqgetnew, reqgetgroupon } from "../../http/api";
//state
const initState = {
  cate: [],
  banner: [],
  goods: [],
  n: 0,
  news:[],
  groupon:[],
};

//action type
const TYPES = {
  HOME_CHANGE_CATE: "HOME_CHANGE_CATE",
  HOME_CHANGE_BANNER: "HOME_CHANGE_BANNER",
  HOME_CHANGE_GOODS: "HOME_CHANGE_GOODS",
  HOME_CHANGE_N: "HOME_CHANGE_N",
  HOME_CHANGE_NEWS:"HOME_CHANGE_NEWS",
  HOME_CHANGE_GROUPON:"HOME_CHANGE_GROUPON",
};

//reducer
const reducer = (state = initState, action) => {
  switch (action.type) {
    //cate
    case TYPES.HOME_CHANGE_CATE:
      return {
        ...state,
        cate: action.cate,
      };
    //banner
    case TYPES.HOME_CHANGE_BANNER:
      return {
        ...state,
        banner: action.banner,
      };
    //goods
    case TYPES.HOME_CHANGE_GOODS:
      return {
        ...state,
        goods: action.goods,
      };
    case TYPES.HOME_CHANGE_N:
      return {
        ...state,
        n: action.n,
      };
    //news
    case TYPES.HOME_CHANGE_NEWS:
        return{
            ...state,
            news:action.news,
        }
    //groupon
    case TYPES.HOME_CHANGE_GROUPON:
        return{
            ...state,
            groupon:action.groupon,
        }    
    default:
      return state;
    
  }
};

//action creator
export const actions = {
  //cate
  changeCate: (cate) => ({ type: TYPES.HOME_CHANGE_CATE, cate }),
  reqCate: () => (dispatch, getState) => {
    let cates = getCate(getState());
    if (cates.length > 0) {
      return;
    }
    reqgetcate().then((res) => {
      dispatch(actions.changeCate(res.data.list));
    });
  },
  //news
  changeNews:(news) => ({ type:TYPES.HOME_CHANGE_NEWS,news }),
  reqNews:() => (dispatch,getState) => {
      let news = getNews(getState());
      if(news.length > 0){
          return;
      }
      reqgetnew().then((res) => {
        dispatch(actions.changeNews(res.data.list));
      });
  },
  //banner
  changeBanner: (banner) => ({ type: TYPES.HOME_CHANGE_BANNER, banner }),
  reqBanner: () => (dispatch, getState) => {
    let banner = getBanner(getState());
    if (banner.length > 0) {
      return;
    }
    reqgetbanner().then((res) => {
      dispatch(actions.changeBanner(res.data.list));
    });
  },
  //groupon
  changegroupon:(groupon) => ({ type:TYPES.HOME_CHANGE_GROUPON,groupon }),
  reqGroupon: () =>(dispatch,getState) =>{
        let groupon = getGroupon(getState());
        if(groupon.length > 0){
            return;
        }
        reqgetgroupon().then((res)=>{
            dispatch(actions.changegroupon(res.data.list));
        })
  },
  //goods
  changeGoods: (goods) => ({ type: TYPES.HOME_CHANGE_GOODS, goods }),
  reqGoods: () => (dispatch, getState) => {
    let goods = getGoods(getState());
    if (goods.length > 0) {
      return;
    }
    reqgetindexgoods().then((res) => {
      dispatch(actions.changeGoods(res.data.list));
    });
  },
  changeN: n =>({ type:TYPES.HOME_CHANGE_N,n })
};

//selector导出数据给组件
export const getCate = (state) => state.home.cate;
export const getBanner = (state) => state.home.banner;
export const getGoods = (state) => state.home.goods;
export const getN = state => state.home.n;
export const getNews = state => state.home.news;
export const getGroupon = state => state.home.groupon;
//导出reducer
export default reducer;
